#include <assert.h>


#define SLEP_DSCAL(alpha,x,i) 	{\
							if (alpha==1.0)\
								;\
							else if (alpha==0. && x->sparsity == SlepDense && x->dim == 1)\
								for((i)=0; (i) < (x->dim) ; ++(i))\
									(x->data.vctr)[(i)] = 0;\
							else if (alpha==0. && x->sparsity == SlepSparse )\
								for((i)=0; (i) < (x->data.spvctr.nnz) ; ++(i))\
									(x->data.spvctr.values)[(i)] = 0;\
							else if(x->sparsity == SlepSparse)\
								for(i=0; i < (x->data.spvctr.nnz) ; i++)\
									(x->data.spvctr.values[i]) *= alpha;\
							else if(x->sparsity == SlepDense)\
								for(i=0; i < (x->dim) ; i++)\
									(x->data.vctr[i*(x->inc)]) *= alpha;\
						}



#define DMEMCPY(dest, orig,size,i)	{\
										for( (i)=0; (i) < (size); ++(i) )\
											(*((dest)+(i))) = (*((orig)+(i)));\
									}



#define DMEMSET(x,value,size,i)		{\
										for((i)=0; (i) < (size) ; ++(i))\
											(*((x)+(i))) = (value);\
									}

#define MVDIMCHECK(A,y)             {\
										if((A)->sparsity== SlepDense && (y)->sparsity==SlepDense)\
											assert((A)->n == (y)->dim);\
										else if( (A)->sparsity == SlepSparse && (y)->sparsity== SlepDense)\
											assert( (A)->n == (y)->dim);\
										else if( (A)->sparsity == SlepSparse && (y)->sparsity== SlepSparse)\
											assert( (A)->n >= ((y)->data.spvctr.index[(y)->data.spvctr.nnz -1]+1) );\
										else if ((A)->sparsity== SlepDense && (y)->sparsity== SlepSparse)\
											assert( (A)->n >= ((y)->data.spvctr.index[(y)->data.spvctr.nnz -1]+1) );\
									}


#define NORMALIZEDMTV(A,b,ATb,opts,b1)		{\
											if( (opts)->nFlag==0)\
												slep_daAxpby(SlepTranspose,1.0,(A),(b),0.,(ATb));\
											else if( (opts)->nFlag == 1){\
												slep_daAxpby(SlepTranspose,1.0,(A),(b),-(slep_dsum((b))),(opts)->mu);\
												slep_invdiagxy( (opts)->nu,(ATb));\
											}else{\
												slep_dvcopy((b),(b1) );\
												slep_invdiagxy( (opts)->nu, (b1));\
												slep_dvcopy((opts)->mu,(ATb) );\
												slep_daAxpby(SlepTranspose,1.,(A),(b1),-(slep_dsum((b1))),(ATb));\
											}	\
									}



#define NORMALIZEDMV(A,x,Ax,opts,x1,i)	{\
											if( opts->nFlag == 0)\
												slep_daAxpby(SlepNoTranspose, 1., (A) , (x) , 0., (Ax) );\
											else if( (opts)->nFlag == 1){\
												slep_dvcopy( (x) , (x1) );\
												slep_invdiagxy((opts)->nu,(x1));\
												assert( (Ax)->sparsity == SlepDense);\
												DMEMSET( (Ax)->data.vctr,1.0,(Ax)->dim, (i) );\
												slep_daAxpby(SlepNoTranspose, 1.0, (A) , (x1),  slep_ddot( (opts)->mu, (x1) ), (Ax) );\
											}else{\
												DMEMSET( (Ax)->data.vctr,1.0,(Ax)->dim, (i) );\
												slep_daAxpby(SlepNoTranspose, 1.0, (A) , (x), slep_ddot((opts)->mu, (x) ), (Ax) );\
												slep_invdiagxy((opts)->nu, (Ax) ); \
											}\
										}